﻿package net.wangit.adm.role;

import net.wangit.LabelUtil;
import net.wangit.context.ActionContext;
import net.wangit.jdbc.DB;
import net.wangit.support.action.TransactionAction;
import net.wangit.user.admin.AdminCache;
import net.wangit.user.group.UserGroupCache;
import net.wangit.user.org.OrgCache;
import net.wangit.user.role.RoleCache;
import net.wangit.util.Checker;

public class DeleteRoleAction extends TransactionAction {

	private static final String CONFIRM = "select * from AROLE where parentid=?";
	private static final String ROLE_DELETE = "delete from AROLE where roleid=?";
	private static final String ADMINISTRATOR_DELETE = "delete from AADMINROLE where roleid=?";
	private static final String ORGANIZATION_DELETE = "delete from AORGROLE where roleid=?";
	private static final String GROUP_DELETE = "delete from AGROUPROLE where roleid=?";
	private static final String USER_DELETE = "delete from AUSERROLE where roleid=?";
	private static final String PRIVILEGE_DELETE = "delete from AROLEPRIVILEGE where roleid=?";

	public DeleteRoleAction() {
	}

	public void doAction(DB db, ActionContext ac) throws Exception {
		String roleId = ac.getRequestParameter("roleid");
		net.wangit.MapList list = db.query(
				"select * from AROLE where parentid=?", roleId, 2);
		if (!Checker.isEmpty(list)) {
			ac.getActionResult().setSuccessful(false);
			ac.getActionResult().addError(
					LabelUtil.get(ac, "adm", "role.has_subnodes", roleId));
		} else {
			db.execute("delete from AROLE where roleid=?", roleId, 2);
			db.execute("delete from AADMINROLE where roleid=?", roleId, 2);
			db.execute("delete from AORGROLE where roleid=?", roleId, 2);
			db.execute("delete from AGROUPROLE where roleid=?", roleId, 2);
			db.execute("delete from AUSERROLE where roleid=?", roleId, 2);
			db.execute("delete from AROLEPRIVILEGE where roleid=?", roleId, 2);
			RoleCache.clear(roleId);
			OrgCache.clearAll();
			UserGroupCache.clearAll();
			AdminCache.clearAll();
		}
	}
}
